package com.linchong.concurrency.atomic;

import com.linchong.concurrency.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-08 12:40
 * @Description: ConcurrencyTest$
 */
@Slf4j
@ThreadSafe
public class AtomicExample5 {

	@Getter
	public volatile int count = 100;

	/**
	 * 更新某个类的某个字段的值，该属性必须是使用Volatile修饰的非static
	 */
	private static AtomicIntegerFieldUpdater<AtomicExample5> updater = AtomicIntegerFieldUpdater
			.newUpdater(AtomicExample5.class, "count");


	public static void main(String[] args) {

	AtomicExample5 example5 = new AtomicExample5();
		if(updater.compareAndSet(example5,100,120)){
			log.info("update success,{}",example5.getCount());
		}

		if(updater.compareAndSet(example5,100,120)){
			log.info("update success,{}",example5.getCount());
		}else{
			log.info("update fail,{}",example5.getCount());
		}
	}
}
